package com.roc.base.mall.configuration.db;

import javax.sql.DataSource;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import com.atomikos.jdbc.AtomikosDataSourceBean;
import com.mysql.cj.jdbc.MysqlXADataSource;

@Configuration
public class DataSourceConfig {


    /**
     * 
     * @return
     */
    @Bean(name="mainDealerDataSource") 
    @Primary
    @Qualifier("mainDealerDataSource") 
    public DataSource mainDealerDataSouce(@Autowired DataSourceMainProperties mainProperties){

    	MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource();  
    	BeanUtils.copyProperties(mainProperties, mysqlXaDataSource);
    	 
        AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
        xaDataSource.setXaDataSource(mysqlXaDataSource);
        xaDataSource.setUniqueResourceName("mainDealerDataSource");
        return xaDataSource;
    }

    /**
     * newerp
     * @return
     */
    @Bean(name="subDataSource") //
    @Qualifier("subDataSource") //
    public DataSource subDealerDataSouce(@Autowired DataSourceSubProperties subProperties){
    	MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource(); 
    	BeanUtils.copyProperties(subProperties, mysqlXaDataSource);
    	
        AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
        xaDataSource.setXaDataSource(mysqlXaDataSource);
        xaDataSource.setUniqueResourceName("subDataSource");
        return xaDataSource;
    }
}
